<template>
  <view>
    <primaryForm :formInfo="primaryForms"></primaryForm>
    <DetailForm :formInfo="detailForms" :belongId="belongId"></DetailForm>
  </view>
</template>

<script lang="ts" setup>
import { kernel, model } from '@/ts/base'
import {  computed } from 'vue'
import primaryForm from './primaryForm.vue';
import DetailForm from './detailForm.vue'
type IProps = {
  formInfo: any
}
const props = defineProps<IProps>()
console.log(props,'propsindex')
const primaryForms = computed(() => {
  const instance: model.InstanceDataModel ={
    data: {},
    node: props.formInfo.instanceData?.node,
    fields: {},
    primary: props.formInfo.instanceData?.primary,
    rules: [],
  }
  props.formInfo.instanceData?.node?.forms.forEach(async (items:any) => {
    if(items.typeName === '主表'){
      instance.fields[items.id] = props.formInfo.instanceData?.fields[items.id]
      instance.data[items.id] = props.formInfo.instanceData?.data[items.id]
      await kernel.createThing(props.formInfo.belong?.id, [], '').then((res) => {
        instance.data[items.id].at(-1).after.push({...res.data})
      })
    }
  })
  return instance
})
let belongId:any = ''
const detailForms = computed(() => {
  const instance: model.InstanceDataModel ={
    data: {},
    node: props.formInfo.instanceData?.node,
    fields: {},
    primary: {},
    rules: [],
  }
  props.formInfo.instanceData?.node?.forms.forEach((element: { typeName: string; id: string | number }) => {
    if(element.typeName === '子表'){
      instance.data[element.id] = props.formInfo.instanceData.data[element.id]
      instance.fields[element.id] = props.formInfo.instanceData.fields[element.id]
      belongId = props.formInfo.belong.id
    }
  })
  return instance
})
</script>

<style scoped></style>